<?php
 
include_once("./configure/configure.php");						//--> global var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("./admin/lib/utils/StringUtils.php");				//--> String utils
include_once("./admin/lib/utils/ImageUtils.php");				//--> image utils
include_once("./admin/lib/utils/StyleUtils.php");				//--> Style utils
include_once("./admin/lib/utils/FileUtils.php");				//--> file utils

include_once("./lib/impl/CouponImpl.php");
include_once("./lib/impl/CouponAttributeImpl.php");

include_once("./lib/impl/CategoryImpl.php");
include_once("./lib/impl/KeywordImpl.php");

include_once("./lib/constant/GlobalConstant.php");


$oStringUtils 	= new StringUtils();
$oImageUtils	= new ImageUtils();

$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
$oCAttributeImpl= new CouponAttributeImpl(DB_TAG_COUPON);
$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);
$oKeywordImpl	= new KeywordImpl(DB_TAG_COUPON);

include_once("includeSmarty.php");

$page 		= $_GET['page'];
$keyword 	= $_GET['keyword'];


$cacheId = $page . '_' . $keyword;

/*----- out html -----*/
$SUB_DIR	= FileUtils::getDirByString($keyword) . '/';

$smarty->compile_dir  	= COMPILE_DIR . '/searches/' . $SUB_DIR;
$smarty->cache_dir  	= CACHE_DIR. '/searches/' . $SUB_DIR;

if(!$smarty->isCached('searches.html', $cacheId, $cacheId)){
	include_once("includeTag.php");
	include_once("includeMerchant.php");
	include_once("includeCategories.php");
	
	if(!empty($keyword)){
		$keyword = trim($keyword);
		
		$where	= " WHERE status = 1 AND MATCH (code, name, descript) AGAINST ('$keyword')";
		
		if(!empty($keyword)){
			$oKeywordImpl->increase($keyword);
		}
//		
//		$keywordId = $oKeywordImpl->getCouponKeywordId($keyword);
//		
//		$where	= " WHERE status = 1 AND id IN (SELECT couponId FROM keywords_coupon_mapping WHERE keywordId = $keywordId)";
	}else{
		$where 	= " WHERE status = 1";
	}
	
	$orderStr	= " ORDER BY startDate DESC, r_sortValue DESC";
	
	
	$oCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 20);
	if(count($oCoupons) == 0){
		$where	= " WHERE status = 1 AND name like '%$keyword%'";
		
		$oCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 20);
	}
	
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
					
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		$oCoupon->setMerchant($oMerchant);
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oImageUtils->calculateNewSize($oImage->getWidth(), $oImage->getHeight(), 72, 72, 30);
			
				$oImage->setWidth($oImageUtils->getWidth());
				$oImage->setHeight($oImageUtils->getHeight());
				
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}
	}
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(10));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());

	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oMerchants', $oMerchants);
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCategories', $oCategories);

}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);

$smarty->assign('isSearch', 'yes');

$smarty->assign('keyword', rawurlencode($keyword));

$smarty->assign('theme', StyleUtils::getTheme());

$smarty->display('searches.html', $cacheId, $cacheId);
?>
